/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model.Dao;
import Model.Entity.CustomerEntity;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author Trung
 */
public class CustomerDao {
    public List<CustomerEntity> getAll(){
        DataAccess da = new DataAccess();
        List<CustomerEntity> listCa = new ArrayList<CustomerEntity>();
        try {
            da.ps = da.con.prepareStatement("select * from UserInfo");
            da.rs = da.ps.executeQuery();
            while(da.rs.next()){
                listCa.add(new CustomerEntity(da.rs.getInt(1),da.rs.getString(2),da.rs.getBoolean(3),da.rs.getBoolean(4),da.rs.getString(5),da.rs.getString(6),da.rs.getInt(7)));
            }
        } catch (SQLException ex) {
            Logger.getLogger(CategoryDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return listCa;       
    }
    
    public Boolean createCustomer(CustomerEntity customer ){
        DataAccess da = new DataAccess();
        Boolean check = false;
        try {
            da.ps = da.con.prepareStatement("insert into UserInfo values ('"+ customer.getName() + "','"+ customer.getSex()+ "','"+ customer.isGrant()+ "','"+ customer.getUserName()+ "','"+ customer.getPass()+ "',"+ customer.getPhone()+ ")");
            check = da.ps.execute();         
        } catch (SQLException ex) {
            Logger.getLogger(CategoryDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return check;       
    }
    
    public Boolean checkCustomer (CustomerEntity customer ){
        DataAccess da = new DataAccess();
        Boolean check = false;
        try {
            da.ps = da.con.prepareStatement("select * from UserInfo where userName = '"+customer.getUserName()+ "'");
            da.rs = da.ps.executeQuery();
            if(da.rs.next()){
                check = true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(CategoryDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return check;  
    }
}
